package com.hxzy.projectssmplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.projectssmplus.vo.AjaxTableInfo;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class BaseController {

    /**
     * 使用PageHelper启动分页
     */
    public IPage startPage(){
        //使用原始servlet来取
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) attributes;

        HttpServletRequest request = servletRequestAttributes.getRequest();

        String pageStr=request.getParameter("page");
        String limtStr=request.getParameter("size");

        int page= Integer.parseInt(pageStr);
        int limit=Integer.parseInt(limtStr);

        //使用mybatis-plus的分页
        return new Page(page,limit);

    }

    public AjaxTableInfo getTableInfo(IPage page){
        AjaxTableInfo  ajaxTableInfo=new AjaxTableInfo();
        ajaxTableInfo.setCode(0);
        ajaxTableInfo.setMsg("OK");

        ajaxTableInfo.setCount( page.getTotal() );
        ajaxTableInfo.setData( page.getRecords());

        return ajaxTableInfo;
    }

}
